package com.sdhs.paas.flux.app;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

import com.sdhs.paas.flux.model.User;

import reactor.core.publisher.Mono;

@RestController
public class RESTClientController {
	@GetMapping("/restclient")
	public String restClient(){
		final User user = new User();
        user.setName("Test");
        user.setUsername("test@example.org");
        final WebClient client = WebClient.create("http://localhost:8087/user");
        final Mono<User> createdUser = client.post()
                .uri("")
                .accept(MediaType.APPLICATION_JSON)
                .body(Mono.just(user), User.class)
                .exchange()
                .flatMap(response -> response.bodyToMono(User.class));
        System.out.println(createdUser.block());
		return "restclient";
	}
	
    public static void main(final String[] args) {
        final User user = new User();
        user.setName("Test");
        user.setUsername("test@example.org");
        final WebClient client = WebClient.create("http://localhost:8087/user");
        final Mono<User> createdUser = client.post()
                .uri("")
                .accept(MediaType.APPLICATION_JSON)
                .body(Mono.just(user), User.class)
                .exchange()
                .flatMap(response -> response.bodyToMono(User.class));
        System.out.println(createdUser.block());
    }
}
